#!/bin/bash

for var in $*
do
if [ "$var" == "-s" ]
then
echo "-s: sending secret code"
adb shell am broadcast -a android.provider.Telephony.SECRET_CODE -d android_secret_code://2010
adb shell am broadcast -a android.provider.Telephony.SECRET_CODE -d android_secret_code://36245
elif [ "$var" == "-c" ]
then
echo "-c: clear previous log"
adb logcat -c
elif [ "$var" == "-r" ]
then
echo "-r: open related logs, Exchange and Email"
pidEmail=`adb shell ps | grep email | cut -c11-16`
pidEmail=`echo $pidEmail`
echo "email pid: $pidEmail"
pidExchange=`adb shell ps | grep -i exchange | cut -c11-16`
pidExchange=`echo $pidExchange`
echo "exchange pid: $pidExchange"
else
echo "-s: sending secret code"
echo "-c: clear previous log"
echo "-r: show only logs in Exchange and Email process"
echo "-h: show this help message"
exit 0
fi
done

echo "verbose"
adb shell setprop log.tag.Serializer VERBOSE

if [ "$pidEmail" != "" ]
then
	if [ "$pidExchange" != "" ]
	then
	echo "both process exist"
	adb logcat -v time | grep "$pidEmail\|$pidExchange"
	else
	echo "only email is alive, press any key to continue... press ctrl+c to stop"
	read -sn 1
	adb logcat -v time | grep $pidEmail
	fi
else
	if [ "$pidExchange" != "" ]
	then
	echo "only exchange is alive, this should not be happening"
	exit 1
	else
	echo "none"
	adb logcat -v time
	fi
fi
